<?php
/*
 * @分销商城
 * @功能说明：脚本服务
 * @更新说明：暂无更新
 * @文件名 SdkController.class.php
 * @编码 UTF-8
 * @创建时间 2016-1-18 下午4:39:28
 * @创建人 XING
 */
namespace dg\Controller;
use Think\Controller;
class SdkController extends Controller {
    /*
	 * 分销商城_OMG脚本
	 * @date: 2016-4-25 下午5:13:21
	 * @editor: XING
	 */
    public function runOmg(){
		for($i=1;$i<=77;$i++){
			$data = file_get_contents('http://wap.100fua.com/Handler/ProductHandler.ashx?action=ShopDefault&math=0.2766594234853983&pageIndex='.$i.'&Keywords=&openId=ooSBXxFlVGQrn1hzd4mg4SzbScSY&pageSize=10');
			$dataArr = json_decode($data,1);
			if($dataArr['result'] == 'success' && $dataArr['List']){
				foreach($dataArr['List'] AS $varr){
					$checkExist = M('OmgPrice')
								->where(array('sku_id'=>$varr['Sku']))->find();
					if($checkExist){
						M('OmgPrice')
							->where(array('sku_id'=>$varr['Sku']))->setField(array(
								'omg_title' => $varr['Name'],
								'omg_price' => $varr['Price'],
								'omg_sales' => $varr['SellCount'],
								'omg_pic' => $varr['ImgUrl'],
								'omg_url' => 'wap.100fua.com/p/'.$varr['Sku'].'.html'
							));
					}else{
						$omgOrder = M('OmgPrice')->add(array(
								'sku_id' => $varr['Sku'],
								'omg_title' => $varr['Name'],
								'omg_pic' => $varr['ImgUrl'],
								'omg_price' => $varr['Price'],
								'omg_sales' => $varr['SellCount'],
								'omg_url' => 'wap.100fua.com/p/'.$varr['Sku'].'.html',
								'add_at' => time()
						));
		
					}
					
					$getTongjiSales = M('OmgTongji')->field('salesall')
									->where('from_unixtime(datetime,"%Y-%m-%d") = "'.date('Y-m-d',time()-24*3600).'" AND sku_id = "'.$varr['Sku'].'"')->find();
					$url = trim($checkExist['kl_url']);
					$klaPrice = '0.00';
					if($url){
						$data = file_get_contents($url);
						preg_match('/(\S)*id\=\"js_currentPrice\">¥<span>(\S*)<\/span><\/span>(\S)*/',$data,$matches);
						if($matches && $matches['2']){
							$klaPrice = $matches['2'];
						}
					}
					M('OmgTongji')->add(array(
							'sku_id' => $varr['Sku'],
							'datetime' => time(),
							'datestr' => date('Y-m-d'),
							'price' => $varr['Price'],
							'salesAll' => $varr['SellCount'],
							'salesToday' => $getTongjiSales && $getTongjiSales['salesall'] ? $varr['SellCount']-$getTongjiSales['salesall'] : 0,
							'kl_price' => $klaPrice,
							'add_at' => time()
						));
				}
			}
		}
    }
    
}